package com.situ.mall.controller.back;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Category;
import com.situ.mall.entity.Result;
import com.situ.mall.service.ICategoryService;

@Controller
@RequestMapping("/category")
public class CategoryController {
	
	@Autowired
	private ICategoryService categoryService;

// 后台
// product添加功能  获取 二级信息
	@RequestMapping("/selectSecondCategoryByTopCategoryId.action")
	@ResponseBody
	public Result selectSecondCategoryByTopCategoryId(Integer topCategoryId ) {
		System.out.println("CategoryController.selectSecondCategoryByTopCategoryId()");
		List<Category> secondCategoryList = categoryService.selectSecondCategoryByTopCategoryId(topCategoryId);
		if (secondCategoryList == null || secondCategoryList.size()==0) {
			return Result.error("二级信息查找失败");
		}else {
			return Result.success("",secondCategoryList);
		}
		
	}
	
	
//0.0.测试
	@RequestMapping("/selectAll.action")
	@ResponseBody
	public List<Category> selectAll() {
		System.out.println("CategoryController.selectAll()");
		return categoryService.selectAll();
	}
	

// 0. index.jsp点一下   跳转到这里    再转发到   Category_list。jsp
	@RequestMapping("/getCategoryListJsp.action")
	public String getCategoryListJsp() {
		System.out.println("CategoryController.getCategoryListJsp()");
		return "category_list";
	}
// 1.查询 每页信息     返回Result到	Category_list.jsp
	@RequestMapping("/selectByPage.action")
	@ResponseBody
	public Result selectByPage( @RequestParam(defaultValue = "1")Integer page,  @RequestParam(defaultValue = "5")Integer limit) {
		System.out.println("CategoryController.selectByPage()");
		return categoryService.selectByPage(page,limit);
	}
	
// 2.批量删除
	@RequestMapping("/deleteAll.action")
	@ResponseBody
	public Result deleteAll(Integer[] ids) {  
		// 批量删除   好多个  所以接收数组
		System.out.println("CategoryController.deleteAll()");
		System.out.println(Arrays.toString(ids));
		
		boolean isSuccess = categoryService.deleteAll(ids);
		if (isSuccess) {
			return Result.success("删除成功");
		} else {
			return Result.error("删除失败");
		}
	}
	
// 3.删除
	@RequestMapping("/deleteById.action")
	@ResponseBody
	public Result deleteById(Integer id) {
		System.out.println("CategoryController.deleteById()");
		System.out.println("id="+id);
		
		boolean isSuccess = categoryService.deleteById(id);
		if (isSuccess) {
			System.out.println("删除成功");
			return Result.success("删除成功");
		}else {
			System.out.println("删除失败");
			return Result.error("删除失败");
		}
	}

//4.添加
	//**************4.1.添加一级分类*************************
	//1. 转发到 添加页面
	@RequestMapping("/getInsertTopCategoryJsp.action")
	public String getInsertTopCategoryJsp(Model model) {
		System.out.println("CategoryController.getInsertTopCategoryJsp()");
		
		List<Category> list = categoryService.getTopCategoryList();
		model.addAttribute("topCategoryList", list);
		
		return "/category_insertTop";
	}
	//2.真正的添加   一级分类 进数据库
	@RequestMapping("/insertTopCategory.action")
	@ResponseBody
	public Result insertTopCategory(Category category) {
		System.out.println("CategoryController.insertTopCategory()");
		System.out.println(category);
		
		return categoryService.insertTopCategory(category);
	}
	
	
	//***************4.2.添加二级分类***************************
	//1.转发到 seacond_category_insert.jsp  添加二级分类的页面
	@RequestMapping("/getInsertSeacondCategoryJsp.action")
	public String getInsertSeacondCategoryJsp(Model model) {
		System.out.println("CategoryController.getInsertSeacondCategoryJsp()");
		
		//1.查出一级分类
		List<Category> topCategoryList = categoryService.getTopCategoryList();
		model.addAttribute("topCategoryList", topCategoryList);
		
		return "/category_insertSeacond";
		
	}
	
//echarts统计
	//1.找到 category_count.jsp页面
	@RequestMapping("/getCategoryCountJsp.action")
	public String getCategoryCountJsp() {
		System.out.println("CategoryController.getCategoryCountJsp()");
		return "category_count";
	}
	
	// 2. category_count.jsp的  ajax,,获取 统计 二级分类 有几个商品  数量
	@RequestMapping("/getCategoryCount.action")
	@ResponseBody
	public Result getCategoryCount() {
		System.out.println("CategoryController.getCategoryCount()");
		
		List<Category> list = categoryService.getCategoryCount();
		System.out.println(list);
		
		return Result.success("",list);
	}
	
}
